package leetcode.code1419;

public class Solution {
	static int[] ids;
	static {
		ids = new int[26];
		ids['c' - 'a'] = 0;
		ids['r' - 'a'] = 1;
		ids['o' - 'a'] = 2;
		ids['a' - 'a'] = 3;
		ids['k' - 'a'] = 4;
	}

	public int minNumberOfFrogs(String croakOfFrogs) {
		int[] croak = new int[5];
		int ans = 0, cnt = 0;
		for (int i = 0; i < croakOfFrogs.length(); i++) {
			int c = ids[croakOfFrogs.charAt(i) - 'a'];
			if (c == 0) {
				croak[c]++;
				ans = Math.max(ans, ++cnt);
			} else if (croak[c - 1] == 0) {
				return -1;
			} else if (c == 4) {
				croak[c - 1]--;
				cnt--;
			} else {
				croak[c - 1]--;
				croak[c]++;
			}
		}
		return cnt == 0 ? ans : -1;
	}
}
